package BinaryTreeTest;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-05-17
 * Time: 14:41
 */
public class BinaryTree {
    //二叉树的结点类
    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val){
            this.val = val;
        }
    }

    //创建一个二叉树
    public TreeNode createBinaryTree(){
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        return A;
    }

    //前序遍历----------> 根-左-右
    public void prevOrder(TreeNode root){
        if(root==null) return;
        System.out.print(root.val+" ");
        prevOrder(root.left);
        prevOrder(root.right);
    }

    //中序遍历------->左-根-右
    public void inOrder(TreeNode root){
        if(root==null) return;
        prevOrder(root.left);
        System.out.print(root.val+" ");
        prevOrder(root.right);
    }

    //后序遍历----------->左-右-根
    public void postOrder(TreeNode root){
        if(root==null) return;
        prevOrder(root.left);
        prevOrder(root.right);
        System.out.print(root.val+" ");
    }

    // 获取树中节点的个数
    //遍历的思路
   // public static int cnt =0;
//    public int size(TreeNode root){
//        if(root==null) return 0;
//        ++cnt;
//        size(root.left);
//        size(root.right);
//        return cnt;
//    }
    /**
     * 子问题思路--->左树节点的个数+右树节点的个数+1
     */
    public int size1(TreeNode root){
        if(root==null) return 0;
        return size1(root.left)+size1(root.right)+1;
    }

    // 获取叶子节点的个数----->遍历思路
//    public static int cnt1 =0;
//    public int getLeafNodeCount(TreeNode root){
//        if(root==null) return 0;
//        if(root.left==null&&root.right==null){
//             ++cnt1;
//        }
//        getLeafNodeCount(root.left);
//        getLeafNodeCount(root.right);
//        return cnt1;
//    }

    /** 子问题思路-求叶子结点个数
     * 获取第K层节点的个数---->左子树的叶子结点个数+右子树的叶子结点个数
     */
    int getKLeafNodeCount1(TreeNode root){
        if(root==null) return 0;
        if(root.left==null&&root.right==null){
            return 1;
        }
        return getKLeafNodeCount1(root.left)+getKLeafNodeCount1(root.right);
    }

// 获取二叉树的高度---->左子树和右子树的最大高度 +1就是数的高度
    int getHeight(TreeNode root){
       if(root==null) return 0;
       //左子树的高度
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return leftH>rightH?leftH+1:rightH+1;
    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, char val){
        if(root==null) return null;
        if(root.val==val) return root;
        TreeNode ret = find(root.left,val);
        if(ret!=null) {
            return ret;
        }
        ret = find(root.right,val);
        if(ret!=null) {
            return ret;
        }
        return null;//没有找到返回null
    }

    //层序遍历----->利用非递归的写法------>使用队列
    void levelOrder1(TreeNode root){
        if(root==null) return;
         Queue<TreeNode> queue = new LinkedList<>();
         queue.offer(root);
         while(!queue.isEmpty()){
             TreeNode cur = queue.poll();
             System.out.print(cur.val+" ");
             if(cur.left!=null) {
                 queue.offer(cur.left);
             }
             if(cur.right!=null) {
                 queue.offer(cur.right);
             }
         }
    }

    //层序遍历
    public void levelOrder(TreeNode root){
        if(root==null) return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){

                queue.offer(cur.right);
            }
        }
    }

    // 判断一棵树是不是完全二叉树
    boolean isCompleteTree(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur==null){
                break;
            }
            queue.offer(cur.left);
            queue.offer(cur.right);
        }
        //接下来看队列里面是否还有结点
        while(!queue.isEmpty()){
            TreeNode cur = queue.poll();
            if(cur!=null){
                return false;
            }
        }

        return true;
    }







}
